home *** CD-ROM | disk | FTP | other *** search
- Option Strict Off
-
- Module Module1
-
- ' Set this to True to generate the ShowAppDomainData.exe assembly
- ' (but requires manual renaming of the exe file)
- #Const GEN_SHOWAPPDOMAINDATAEXE = False
-
- Sub Main()
-
- #If GEN_SHOWAPPDOMAINDATAEXE Then
- ' If inside the ShowAppDomainData.Exe program, show
- ' AppDomain properties and exit.
- TestAppDomainProperties
- Exit Sub
- #End If
-
- 'TestAppDomainProperties()
- 'TestCreateDomain()
- TestCreateInstance()
- 'TestProcessExit()
- 'TestUnhandledException()
-
- ' These statements are usuful when running inside Visual Studio.NET
- Console.WriteLine("")
- Console.WriteLine(">>> Press Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' this procedure displays AppDomain properties
-
- Sub TestAppDomainProperties()
- ' Get a reference to the running AppDomain.
- Dim ad As AppDomain = AppDomain.CurrentDomain
-
- ' Print main properties.
- Console.WriteLine("----- AppDomain Data ------------------")
- Console.WriteLine("BaseDirectory = {0}", ad.BaseDirectory)
- Console.WriteLine("FriendlyName = {0}", ad.FriendlyName)
- Console.WriteLine("RelativeSearchPath = {0}", ad.RelativeSearchPath)
- Console.WriteLine("ShadowCopyFiles = {0}", ad.ShadowCopyFiles)
- Console.WriteLine("CurrentThreadId = {0}", AppDomain.GetCurrentThreadId)
-
- ' Display additional information from the AppDomainSetup object.
- Dim ads As AppDomainSetup = ad.SetupInformation
-
- Console.WriteLine("ApplicationName = {0}", ads.ApplicationName)
- Console.WriteLine("CachePath = {0}", ads.CachePath)
- Console.WriteLine("ConfigurationFile = {0}", ads.ConfigurationFile)
- Console.WriteLine("DisallowPublisherPolicy = {0}", ads.DisallowPublisherPolicy)
- Console.WriteLine("LoaderOptimization = {0}", ads.LoaderOptimization)
- Console.WriteLine("PrivateBinPath = {0}", ads.PrivateBinPath)
- Console.WriteLine("ShadowCopyFiles = {0}", ads.ShadowCopyFiles)
-
- ' Display the list of loaded files
- Dim asm As Reflection.Assembly
- Console.WriteLine("Assemblies loaded in current domain")
- For Each asm In ad.GetAssemblies
- Console.WriteLine(" " & asm.FullName)
- Next
- Console.WriteLine("----- End of AppDomain Data ------------")
- End Sub
-
- ' this procedure tests AppDomain creation
-
- ' IMPORTANT: this routine assumes that you have an EXE named
- ' ShowAppDomainData.exe in the BIN directory. If you don't, you
- ' can copy the file found in the project's main directory.
-
- ' This executable file can be created by setting the
- ' GEN_SHOWAPPDOMAINDATAEXE constant to True, compiling the
- ' source and then renaming the EXE to ShowAppDomainData.exe
-
- Sub TestCreateDomain()
- ' Prepare setup info
- Dim ads As New AppDomainSetup()
- ads.ApplicationName = "NewApplication"
- ads.ApplicationBase = "c:\windows\temp"
- ads.PrivateBinPath = "bins;assemblies"
- ' Create the new AppDomain
- Dim ad As AppDomain = AppDomain.CreateDomain("NewAppDomain", Nothing, ads)
-
- ' Run an assembly inside the appdomain
- ad.ExecuteAssembly("ShowAppDomainData.exe")
-
- ' Unload the AppDomain
- AppDomain.Unload(ad)
- End Sub
-
- ' this procedure tests the CreateInstanceFrom method
-
- ' IMPORTANT: it relies on the presence of the TestAssembly.Dll
- ' file in the BIN subdirectory. If this file isn't there, you can copy
- ' it from the main project's directory, or you can recompile the
- ' TestAssembly project
-
- Sub TestCreateInstance()
- ' Create the new AppDomain
- Dim ad As AppDomain = AppDomain.CreateDomain("NewAppDomain")
- ' Load
- Dim oh As System.Runtime.Remoting.ObjectHandle
- Dim o As Object
-
- oh = ad.CreateInstanceFrom("TestAppDomain.Dll", "TestAppDomain.AppDomainData")
- o = oh.Unwrap
- Console.WriteLine("ThreadID = {0} (current={1})", o.ThreadID, AppDomain.GetCurrentThreadId)
- Console.WriteLine("AppName = {0}", o.AppName)
- Console.WriteLine("ExeName= {0}", o.ExeName)
-
- oh = ad.CreateInstanceFrom("TestAppDomain.Dll", "TestAppDomain.AppDomainData2")
- o = oh.Unwrap
- Console.WriteLine("ThreadID = {0} (current={1})", o.ThreadID, AppDomain.GetCurrentThreadId)
- Console.WriteLine("AppName = {0}", o.AppName)
- Console.WriteLine("ExeName= {0}", o.ExeName)
- End Sub
-
- ' this procedure tests the ProcessExit event
-
- Dim WithEvents CurrAppDomain As AppDomain
- Public AppIsExiting As Boolean
-
- Sub TestProcessExit()
- ' Prepare to trap AppDomain events.
- CurrAppDomain = AppDomain.CurrentDomain
- ' Create a DataClass instance that will be collected right now
- Dim dc As New DataClass()
- dc = Nothing
- GC.Collect()
- ' Create an instance of DataClass that will be collected when the application exits.
- Dim dc2 As New DataClass()
- End Sub
-
- Public Sub CurrAppDomain_ProcessExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles CurrAppDomain.ProcessExit
- Console.WriteLine("Exiting...")
- AppIsExiting = True
- End Sub
-
- ' this procedure tests unhandled exceptions on secondary
- ' and main threads, and the UnhandledException event
-
- Sub TestUnhandledException()
- ' Prepare to trap AppDomain events.
- CurrAppDomain = AppDomain.CurrentDomain
-
- ' Cause an exception on a secondary thread
- Dim t As New Threading.Thread(AddressOf ThrowException)
- t.Start()
- Threading.Thread.Sleep(500)
-
- ' Cause a fatal exception on the main thread
- Throw New IndexOutOfRangeException()
-
- End Sub
-
- Sub ThrowException()
- Throw New DivideByZeroException()
- End Sub
-
- Sub CurrAppDomain_UnhandledException(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs) Handles CurrAppDomain.UnhandledException
- ' get a reference to the exception
- Dim ex As Exception = DirectCast(e.ExceptionObject, Exception)
- ' Show information on the current exception.
- Console.WriteLine("{0} (IsTerminating={1})", ex.Message, e.IsTerminating)
- End Sub
- End Module
-
- Class DataClass
- Public ID As Integer
-
- Protected Overrides Sub Finalize()
- Console.WriteLine("Finalizing DataClass (AppIsExiting is {0})", AppIsExiting)
- End Sub
- End Class
-
-